package pro.common.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
import java.util.logging.Logger;

public abstract class BaseCmd {
    protected BaseCmd(String basepath){
        this.basePath = basepath;
    }
    protected  static Logger logger = null;
    protected  String basePath;
    protected  String dicFullPath;

    public void setDicPath(String dicPath){
        File baseFile = new File(this.basePath);
        File  dicFile = new File(baseFile, dicPath);

        this.dicFullPath = dicFile.getPath();

        File file = new File(this.dicFullPath);

        if (!file.exists()){
            file.mkdir();
        }
    }

    public abstract Object read(String fileName);

    public abstract Boolean write(String fileName, Object obj);

    public String readFile(String fileName){
        StringBuilder sb = new StringBuilder();

        try(Scanner sc = new Scanner(new FileReader(this.dicFullPath.concat(fileName)))){
            sc.useDelimiter("\n");
            while (sc.hasNext()){
                sb.append(sc.next());
            }
        }catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        return sb.toString();
    }
}
